/**
  ******************************************************************************
  * @file    main.c
  * @author  MCU Application Team
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) Puya Semiconductor Co.
  * All rights reserved.</center></h2>
  *
  * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include <rthw.h>
#include <rtthread.h>
#include "button.h"
#include "led.h"
#include "bsp_wdg.h"
#include "Bsp_Uart/Bsp_Uart.h"
#include "adc/bsp_adc_tempsensor.h"
/* Private define ------------------------------------------------------------*/
static const char *TAG = "APP_MAIN";
/* Private variables ---------------------------------------------------------*/
/* Private user code ---------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static void idle_hook(void);

static void idle_hook(void);
static struct rt_thread button_thread;
static char button_thread_stack[512];
static void button_thread_entry(void *parameter);

static struct rt_thread LED_thread;
static char LED_thread_stack[256];
static void LED_thread_entry(void *parameter);

void get_uuid(void);
/**
  * @brief  应用程序入口函数.
  * @retval int
  */
int main(void)
{
    /* 初始化所有外设，Flash接口，SysTick */

    rt_err_t rst;
	
    /* IWDG 端口初始化 */
    Bsp_IWDG_Init();
		USART2_Config(115200);
    LOG_W("%s: PYF030 开机测试例程！！！\r\n",TAG);
	
	  get_uuid();
	/* ADC_TempSensor 初始化 */
    //Bsp_ADC_TempSensor_Init();
	    /*?? ??*/
    rst = rt_thread_init(&button_thread,
                         "button",
                         button_thread_entry,
                         RT_NULL,
                         &button_thread_stack[0],
                         sizeof(button_thread_stack),
                         3,
                         5);
    if(rst == RT_EOK)
    {
        rt_thread_startup(&button_thread);
    }
		
		
					/*SHOW 线程*/
		rst = rt_thread_init(&LED_thread,
						"LED",
						LED_thread_entry,
						RT_NULL,
						&LED_thread_stack[0],
						sizeof(LED_thread_stack),
						2,
						20);
	if(rst == RT_EOK)
	{
		rt_thread_startup(&LED_thread);
	}	
		
    rt_thread_idle_sethook(idle_hook);
	
    while (1)
    {
    //WRITE_REG(Uart2_Handle.Instance->DR,'A');
		//HAL_ADC_Start_IT(&AdcHandle);   
		rt_thread_mdelay(1000);
    }		
}

/**
  * @brief  SWITCH????
  * @param  *parameter
  * @retval None
  */
static void button_thread_entry(void *parameter)
{
					
   

    btn_init();
    rt_thread_mdelay(50);
    while(1)
    {


        button_ticks();
        rt_thread_mdelay(25);
        //rt_thread_mdelay(250);

    }
}

/**
  * @brief  LED线程函数
  * @param  *parameter
  * @retval None
  */
static void LED_thread_entry(void *parameter)
{ 
	
	
	
	while(1)
	{	
		LED_ticks();
    //rt_thread_mdelay(50);			
  } 
	
}
/* 空闲任务钩子函数 */
static void idle_hook(void)
{
     /* 喂狗 */
        HAL_IWDG_Refresh(&IwdgHandle);

}

void get_uuid(void)
{
#define UNIQUE_ID       0x1FFF0E00  // 来自中文手册 4.3
#define MCU_UUID_LENGTH 16
rt_uint8_t UUID[MCU_UUID_LENGTH]={0};
    uint8_t il;

    for (il = 0; il < MCU_UUID_LENGTH; il++)
    {
        UUID[il] = *(uint8_t *)(UNIQUE_ID + il);
    }
		
		    rt_kprintf("UUID = ");
    for (il = 0; il < MCU_UUID_LENGTH; il++)
    {
        rt_kprintf("%02X", UUID[il]);
    }
    rt_kprintf("\r\n");
}


/**
  * @brief  错误执行函数
  * @param  无
  * @retval 无
  */
void APP_ErrorHandler(void)
{
  /* 无限循环 */
  while (1)
  {
		
  }
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  输出产生断言错误的源文件名及行号
  * @param  file：源文件名指针
  * @param  line：发生断言错误的行号
  * @retval 无
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* 用户可以根据需要添加自己的打印信息,
     例如: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* 无限循环 */
  while (1)
  {
  }
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT Puya *****END OF FILE******************/
